package com.wei.czz.framework.admin.manager;

import com.wei.czz.common.dto.admin.roleSpace.RoleSpaceListDto;
import com.wei.czz.common.dto.admin.roleSpace.RoleSpacePageEnumDto;
import com.wei.czz.common.enums.CommonEnum;
import com.wei.czz.common.tool.PageDto;
import com.wei.czz.common.utils.CopyUtils;
import com.wei.czz.common.vo.admin.roleSpace.RoleSpaceFormVo;
import com.wei.czz.framework.admin.entity.RoleGroupEntity;
import com.wei.czz.framework.admin.entity.RoleSpaceEntity;
import com.wei.czz.framework.admin.entity.UserRoleGroupEntity;
import com.wei.czz.framework.admin.service.RoleGroupService;
import com.wei.czz.framework.admin.service.RoleSpaceService;
import com.wei.czz.framework.admin.service.UserRoleGroupService;
import com.wei.czz.framework.common.service.AsyncService;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.stream.Collectors;

/**
 * Created by IntelliJ IDEA.
 *
 * @author wyw
 * date: 2024-10-22 17:29:16
 * className: RoleSpaceManager
 * version: 1.0
 * description:
 */
@Component
@AllArgsConstructor
public class RoleSpaceManager {

    private static final Logger log = LoggerFactory.getLogger(RoleSpaceManager.class);

    private final RoleSpaceService roleSpaceService;

    private final RoleGroupService roleGroupService;

    private final UserRoleGroupService userRoleGroupService;
    private final AsyncService asyncService;

    public PageDto<RoleSpaceListDto> getPageList(RoleSpaceFormVo formVo) {

        /*
            分页获取
         */
        PageDto<RoleSpaceEntity> pageDto = roleSpaceService.getPageList(formVo);
        if (pageDto.isEmpty()) {
            log.info("分页查询结果为空");
            return pageDto.rebuild();
        }

        List<RoleSpaceEntity> roleSpaceList = pageDto.getList();
        // 收集主键
        List<Long> spaceIdList = CopyUtils.mapList(roleSpaceList, RoleSpaceEntity::getId);

        /*
            获取角色授权组与用户列表
         */
        List<UserRoleGroupEntity> userRoleGroupList = userRoleGroupService.findList(spaceIdList, null,
                CommonEnum.ZERO.getValue());
        Map<Long, Long> userMap = userRoleGroupList.stream()
                .collect(Collectors.groupingBy(UserRoleGroupEntity::getSpaceId, Collectors.counting()));

        /*
            获取角色授权组与角色列表
         */
        List<RoleGroupEntity> roleGroupList = roleGroupService.findList(spaceIdList, null,
                CommonEnum.ZERO.getValue());
        Map<Long, Long> map = roleGroupList.stream()
                .collect(Collectors.groupingBy(RoleGroupEntity::getSpaceId, Collectors.counting()));

        // 列表映射
        BiConsumer<RoleSpaceEntity, RoleSpaceListDto> biConsumer = (roleSpace, roleSpaceListDto) -> {
            roleSpaceListDto.setUserCount(userMap.getOrDefault(roleSpace.getId(), CommonEnum.ZERO.getLongValue()))
                    .setRoleCount(map.getOrDefault(roleSpace.getId(), CommonEnum.ZERO.getLongValue()))
                    .setStatusName(CommonEnum.getStatusName(roleSpace.getStatus()));
        };
        List<RoleSpaceListDto> list = CopyUtils.mapList(roleSpaceList, RoleSpaceListDto.class, biConsumer);

        return pageDto.rebuild(list);
    }

    public RoleSpacePageEnumDto getPageEnum() {
        RoleSpacePageEnumDto roleSpacePageEnumDto = new RoleSpacePageEnumDto();
        roleSpacePageEnumDto.setStatusEnumList(CommonEnum.getStatusList());
        return roleSpacePageEnumDto;
    }

    public void delete(List<Long> idList) {

        /*
            删除角色授权组
         */
        roleSpaceService.delete(idList);

        asyncService.execute(() -> {
            /*
                删除角色授权组与角色关联数据
             */
            roleGroupService.delete(idList, null);

            /*
                删除角色授权组与用户关联数据
             */
            userRoleGroupService.delete(idList, null);
        });
    }

}
